//throw exception in destructor
//destructor will be called from normal call[not called during exception handling
//terminate() won't be called!If it is newed and deleted as a object in the heap
#include <iostream>
#include <exception>
using namespace std;
class Myexception : public exception{
public:
  const char* what() const throw() { return "Myexception";}
};

class A{
public:
  A(){cout<<"A()"<<endl; }
  ~A();
};

inline A::~A()
{
  cout<<"~A()"<<endl;
  if(!uncaught_exception()){
    throw Myexception();
  }
}

A* foo()
{
  A* a = new A;
  return a;
}

int main()
{
  A* a;
  try {
    a = foo();
    delete[] a;
  }
  catch (exception& e) {//caught it by reference
    cout<<"Caught Exception:"<<e.what()<<endl;
  }
  return 0;
}
